package uy.edu.um.ltic.server.persistence;

import java.util.ArrayList;

import uy.edu.um.ltic.exception.DataBaseException;
import uy.edu.um.ltic.server.business.Employee;
import uy.edu.um.ltic.server.pool.DataBaseConnectionMgr;

import java.sql.*;

import javax.swing.JOptionPane;

public class EmployeeDAOMgr implements EmployeeDAOMgt {

	private static EmployeeDAOMgr instance = null;

	private static final String SELEMPALLEM = "e.name, e.surname, e.document, "
			+ "e.nEmployee, e.positionInCompany, e.currentCountry, e.branch, "
			+ "e.department, e.username, e.password, e.documentType, e.image, "
			+ "e.availability, e.email, e.administrator, e.salt";

	private EmployeeDAOMgr() {
	}

	public static EmployeeDAOMgr getInstance() {
		if (instance == null) {
			instance = new EmployeeDAOMgr();
		}
		return instance;
	}

	@Override
	public void addEmployee(Employee oEmployee) throws DataBaseException {
		Connection oConnection = this.openConnection();
		PreparedStatement oPreparedStatement;
		String sInsert = "INSERT INTO Employees (name, surname, document, nEmployee, "
				+ "positionInCompany, currentCountry, branch, department, "
				+ "username, password, documentType, image, availability, email, administrator, salt) "
				+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			/*
			 * Blob blob = oConnection.createBlob(); if (oEmployee.getImage() !=
			 * null) { blob.setBytes(1, oEmployee.getImage()); }
			 */
			oPreparedStatement = oConnection.prepareStatement(sInsert);

			oPreparedStatement.setString(1, oEmployee.getName());
			oPreparedStatement.setString(2, oEmployee.getSurname());
			oPreparedStatement.setInt(3, oEmployee.getDocument());
			oPreparedStatement.setInt(4, oEmployee.getnEmployee());
			oPreparedStatement.setString(5, oEmployee.getPositionInCompany());
			oPreparedStatement.setString(6, oEmployee.getCurrentCountry());
			oPreparedStatement.setString(7, oEmployee.getBranch());
			oPreparedStatement.setString(8, oEmployee.getDepartment());
			oPreparedStatement.setString(9, oEmployee.getUsername());
			oPreparedStatement.setString(10, oEmployee.getPassword());
			oPreparedStatement.setString(11, oEmployee.getDocumentType());
			oPreparedStatement.setBytes(12, oEmployee.getImage());
			oPreparedStatement.setString(13, oEmployee.getAvailability());
			oPreparedStatement.setString(14, oEmployee.getEmail());
			oPreparedStatement.setBoolean(15, oEmployee.isAdministrator());
			oPreparedStatement.setString(16, oEmployee.getSalt());

			oPreparedStatement.execute();
		} catch (SQLException e) {
			throw new DataBaseException();
		} finally {
			try {
				oConnection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void deleteEmployee(int nEmployee) throws DataBaseException {
		Connection oConnection = this.openConnection();
		String sDelete = "DELETE FROM Employees WHERE nEmployee = ?";
		PreparedStatement oPreparedStatement;
		try {
			oPreparedStatement = oConnection.prepareStatement(sDelete);
			oPreparedStatement.setInt(1, nEmployee);
			oPreparedStatement.execute();
			
		} catch (SQLException e) {
			throw new DataBaseException();
		}finally{
			try {
				oConnection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/*
	 * private String getUsername(Connection oConnection, int nEmployee) {
	 * String sGet = "SELECT username FROM Employees WHERE nEmployee = ?";
	 * PreparedStatement oPreparedStatement; String result = ""; try {
	 * oPreparedStatement = oConnection.prepareStatement(sGet);
	 * oPreparedStatement.setInt(1, nEmployee); ResultSet oResultSet =
	 * oPreparedStatement.executeQuery(); while (oResultSet.next()) { result =
	 * oResultSet.getString(1); } } catch (SQLException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); } return result; }
	 */

	@Override
	public void editEmployee(Employee oEmployee) throws DataBaseException {

		Connection oConnection = this.openConnection();
		String editEmployees = "UPDATE Employees SET name = ?, "
				+ "surname = ?, " + "document = ?, "
				+ "positionInCompany = ?, " + "currentCountry = ?, "
				+ "branch = ?, " + "department = ?, "
				+ "documentType = ?, " + "image = ?, "
				+ "availability = ?, " + "email = ?, "
				+ "administrator = ? " + "WHERE nEmployee = ?";
		PreparedStatement oPreparedStatement2;
		try {
			oPreparedStatement2 = oConnection.prepareStatement(editEmployees);
			oPreparedStatement2.setString(1, oEmployee.getName());
			oPreparedStatement2.setString(2, oEmployee.getSurname());
			oPreparedStatement2.setInt(3, oEmployee.getDocument());
			oPreparedStatement2.setString(4, oEmployee.getPositionInCompany());
			oPreparedStatement2.setString(5, oEmployee.getCurrentCountry());
			oPreparedStatement2.setString(6, oEmployee.getBranch());
			oPreparedStatement2.setString(7, oEmployee.getDepartment());
			oPreparedStatement2.setString(8, oEmployee.getDocumentType());
			oPreparedStatement2.setBytes(9, oEmployee.getImage());
			oPreparedStatement2.setString(10, oEmployee.getAvailability());
			oPreparedStatement2.setString(11, oEmployee.getEmail());
			oPreparedStatement2.setBoolean(12, oEmployee.isAdministrator());
			oPreparedStatement2.setInt(13, oEmployee.getnEmployee());
			oPreparedStatement2.execute();
		} catch (SQLException e) {
			throw new DataBaseException();
		} finally {
			try {
				oConnection.close();
			} catch (SQLException e) {
				throw new DataBaseException();
			}
		}

	}

	/*
	 * public void createTableUsername() { Connection oConnection =
	 * this.openConnection(); String createTableUserPassword =
	 * "CREATE TABLE UserPassword(username VARCHAR(20) PRIMARY KEY," +
	 * "password VARCHAR(20) NOT NULL)"; Statement oStatement; try { oStatement
	 * = oConnection.createStatement();
	 * oStatement.execute(createTableUserPassword); oConnection.close(); } catch
	 * (SQLException e) { e.printStackTrace(); } }
	 */

	public void createTableEmployees() throws DataBaseException {
		Connection oConnection = this.openConnection();
		String createTableEmployees = "CREATE TABLE Employees(name VARCHAR(20),"
				+ "surname VARCHAR(30),"
				+ "document INT NOT NULL,"
				+ "nEmployee INT PRIMARY KEY,"
				+ "positionInCompany VARCHAR(30),"
				+ "currentCountry VARCHAR(30),"
				+ "branch VARCHAR(30),"
				+ "department VARCHAR(30),"
				+ "username VARCHAR(20) UNIQUE,"
				+ "password VARCHAR(50),"
				+ "documentType VARCHAR(20),"
				+ "image LONGBLOB,"
				+ "availability VARCHAR(30),"
				+ "email VARCHAR(30) UNIQUE,"
				+ "administrator BOOLEAN,"
				+ "salt VARCHAR(30), "
				+ "FOREIGN KEY (currentCountry) REFERENCES General(name) ON DELETE NO ACTION ON UPDATE CASCADE, "
				+ "FOREIGN KEY (positionInCompany) REFERENCES General(name) ON DELETE NO ACTION ON UPDATE CASCADE, "
				+ "FOREIGN KEY (branch) REFERENCES General(name) ON DELETE NO ACTION ON UPDATE CASCADE, "
				+ "FOREIGN KEY (department) REFERENCES General(name) ON DELETE NO ACTION ON UPDATE CASCADE);";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			oStatement.execute(createTableEmployees);
			
		} catch (SQLException e) {
			throw new DataBaseException();
		}finally{
			try {
				oConnection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public ArrayList<Employee> getEmployees(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		if ((oEmployee.getName() == null || oEmployee.getName().equals(""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNameNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesBranchNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesPositionInCompanyNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesCurrentCountryNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNameBranchNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNameBranchPosNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNameBranchCurNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNameCurNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesBranchPosNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesPosCurNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNamePosCurNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesBranchPosCurNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesBranchCurNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNamePosNull(oEmployee);
		} else if ((oEmployee.getName() != null || !oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() != null || !oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() != null || !oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() != null || !oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesNoneNull(oEmployee);
		} else if ((oEmployee.getName() == null || oEmployee.getName().equals(
				""))
				&& (oEmployee.getBranch() == null || oEmployee.getBranch()
						.equals(""))
				&& (oEmployee.getPositionInCompany() == null || oEmployee
						.getPositionInCompany().equals(""))
				&& (oEmployee.getcurrentCountry() == null || oEmployee
						.getcurrentCountry().equals(""))) {
			oList = getEmployeesAllNull(oEmployee);
		}

		return oList;
	}

	@Override
	public Connection openConnection() throws DataBaseException {
		Connection oConnection = null;
		try {
			oConnection = DataBaseConnectionMgr.getInstance().getConnection();
		} catch (SQLException e2) {
			throw new DataBaseException();
		}
		return oConnection;
	}

	private ArrayList<Employee> getEmployeesNameNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany()
				+ "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%' AND branch LIKE '"
				+ oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();

		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesPositionInCompanyNull(
			Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (name LIKE '" + oEmployee.getName()
				+ "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%' AND branch LIKE '"
				+ oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesCurrentCountryNull(
			Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany() + "%' AND name LIKE '"
				+ oEmployee.getName() + "%' AND branch LIKE '"
				+ oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesBranchNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany()
				+ "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%' AND name LIKE '"
				+ oEmployee.getName() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNameBranchNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany()
				+ "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesPosCurNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (name LIKE '" + oEmployee.getName()
				+ "%' AND branch LIKE '" + oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNamePosCurNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (branch LIKE '"
				+ oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesBranchPosCurNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (name LIKE '" + oEmployee.getName()
				+ "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesBranchCurNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (name LIKE '" + oEmployee.getName()
				+ "%' AND positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNamePosNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (branch LIKE '"
				+ oEmployee.getBranch() + "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesAllNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM + " FROM Employees e";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNoneNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (name LIKE '" + oEmployee.getName()
				+ "%' AND positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany()
				+ "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%' AND branch LIKE '"
				+ oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNameBranchCurNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNameCurNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (positionInCompany LIKE '"
				+ oEmployee.getPositionInCompany() + "%' AND branch LIKE '"
				+ oEmployee.getBranch() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesNameBranchPosNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oList;
	}

	private ArrayList<Employee> getEmployeesBranchPosNull(Employee oEmployee) throws DataBaseException {
		ArrayList<Employee> oList = new ArrayList<Employee>();
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE (name LIKE '" + oEmployee.getName()
				+ "%' AND currentCountry LIKE '"
				+ oEmployee.getcurrentCountry() + "%')";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							null, oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				} else {
					oList.add(new Employee(oResult.getString(1), oResult
							.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5), oResult
									.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9), oResult
									.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16)));
				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oList;
	}

	public void dropTable() throws DataBaseException { // SOLO PARA QUE FUNCIONEN LOS UNIT TESTS!! SE
								// PUEDE BORRAR. CAMBIAR EN MGT TAMBIEN
		Connection oConnection = this.openConnection();
		String dropTable = "DROP TABLE Employees CASCADE";
		try {
			Statement oStatement = oConnection.createStatement();
			oStatement.execute(dropTable);
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
	}

	@Override
	public Employee getEmployeeLogIn(String username) throws DataBaseException {
		Employee oEmployeeN = null;
		Connection oConnection = this.openConnection();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE e.username = '" + username + "'";
		Statement oStatement;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oEmployeeN = new Employee(oResult.getString(1),
							oResult.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5),
							oResult.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9),
							oResult.getString(10), oResult.getString(11), null,
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16));
				} else {
					oEmployeeN = new Employee(oResult.getString(1),
							oResult.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5),
							oResult.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9),
							oResult.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16));

				}
			}
			oResult.close();
			oConnection.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		}
		return oEmployeeN;
	}

	public void setStatus(int nEmployee, String status) throws DataBaseException {
		Connection oConnection = this.openConnection();
		PreparedStatement oPreparedStatement;
		String query = "UPDATE Employees SET availability = ? WHERE nEmployee = ?";
		try {
			oPreparedStatement = oConnection.prepareStatement(query);
			oPreparedStatement.setString(1, status);
			oPreparedStatement.setInt(2, nEmployee);
			oPreparedStatement.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				oConnection.close();
			} catch (SQLException e) {
				 throw new DataBaseException();
			}
		}
	}

	public ArrayList<Employee> getAllEmployees() throws DataBaseException {
		Connection oConnection = this.openConnection();
		ArrayList<Employee> oList = new ArrayList<Employee>();
		String sGet = "SELECT " + SELEMPALLEM + " FROM Employees e";
		Statement oStatement;
		Employee oEmployeeN = null;
		try {
			oStatement = oConnection.createStatement();
			ResultSet oResult = oStatement.executeQuery(sGet);
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					oEmployeeN = new Employee(oResult.getString(1),
							oResult.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5),
							oResult.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9),
							oResult.getString(10), oResult.getString(11), null,
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16));
					oList.add(oEmployeeN);
				} else {

					oEmployeeN = new Employee(oResult.getString(1),
							oResult.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5),
							oResult.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9),
							oResult.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16));
					oList.add(oEmployeeN);
				}
			}
			oResult.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		} finally {
			try {
				oConnection.close();
			} catch (SQLException e) {
				 throw new DataBaseException();
			}
		}
		return oList;
	}

	@Override
	public Employee getEmployeeNEmp(int nEmployee) throws DataBaseException {
		Connection oConnection = this.openConnection();
		Employee result = new Employee();
		String sGet = "SELECT " + SELEMPALLEM
				+ " FROM Employees e WHERE nEmployee = ?";
		PreparedStatement oPreparedStatement;
		try {
			oPreparedStatement = oConnection.prepareStatement(sGet);
			oPreparedStatement.setInt(1, nEmployee);
			ResultSet oResult = oPreparedStatement.executeQuery();
			while (oResult.next()) {
				if (oResult.getBlob(12) == null) {
					result = new Employee(oResult.getString(1),
							oResult.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5),
							oResult.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9),
							oResult.getString(10), oResult.getString(11), null,
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16));
				} else {
					result = new Employee(oResult.getString(1),
							oResult.getString(2), oResult.getInt(3),
							oResult.getInt(4), oResult.getString(5),
							oResult.getString(6), oResult.getString(7),
							oResult.getString(8), oResult.getString(9),
							oResult.getString(10), oResult.getString(11),
							oResult.getBlob(12).getBytes(1,
									(int) oResult.getBlob(12).length()),
							oResult.getString(13), oResult.getString(14),
							oResult.getBoolean(15), oResult.getString(16));
				}
			}
			oResult.close();
		} catch (SQLException e) {
			 throw new DataBaseException();
		} finally {
			try {
				oConnection.close();
			} catch (SQLException e) {
				throw new DataBaseException();
			}
		}
		return result;
	}
}